package vivek.project.pocketmoney.common.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.events.ValidationResultEvent;
	import mx.formatters.NumberFormatter;
	import vivek.project.pocketmoney.common.valdiators.AdvancedCurrencyValidator;

	public class CurrencyControl extends TextInput
	{

		public function CurrencyControl()
		{
			super();
			text="0.00";
			this.currencyValidator.source=this;
			this.currencyValidator.property="text";
			this.currencyValidator.precision=2;
			this.currencyValidator.allowNegative=false;
			this.currencyValidator.required=true;
			this.currencyValidator.minValue=0.00;
			formatter.precision=2;
			button.label="E";
			button.x=10;
			button.y=5;
			button.width=10;
			button.height=10;
			setStyle("textAlign", "right");
		}
		private var _precision:int=2;
		private var _zeroAllowed:Boolean=false;
		private var button:Button=new Button();
		private var currencyValidator:AdvancedCurrencyValidator=new AdvancedCurrencyValidator();
		private var formatter:NumberFormatter=new NumberFormatter();
		private var validator:ValidationResultEvent;

		public function get precision():int
		{
			return _precision
		}

		public function set precision(precision:int):void
		{
			_precision=precision;
			this.currencyValidator.precision=_precision;
			formatter.precision=_precision;
		}

		public function validateIt(event:Event):Boolean
		{
			var suppressEvents:Boolean=false;
			var currentTarget:DisplayObject=event.currentTarget as DisplayObject;
			var sourceTarget:DisplayObject=this;
			suppressEvents=sourceTarget == currentTarget;
			validator=currencyValidator.validate(text, suppressEvents);
			if (validator.type == ValidationResultEvent.VALID)
			{
				this.errorString="";
				setStyle("backgroundColor", "#FFFFFF");
			}
			else
			{
				this.errorString=validator.message;
				setStyle("backgroundColor", "#FF0000");
			}
			return (validator.type == ValidationResultEvent.VALID)
		}

		public function get zeroAllowed():Boolean
		{
			return _zeroAllowed;
		}

		public function set zeroAllowed(value:Boolean):void
		{
			_zeroAllowed=value;
			currencyValidator.zeroAllowed=value;
		}
	}
}